package org.blueoxygen.cimande2.ma.gene.manager;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.blueoxygen.cimande2.ma.gene.entity.Person;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.util.Log;

public class ConnectionManager {
	JSONObject personalData, father, mother;
	JSONArray data, childs;
	Person personFather, personMother;

	public ArrayList<Person> getPersonList(String url) {
		connect2(url);
		ArrayList<Person> persons = new ArrayList<Person>();
		for (int i = 0; i < data.length(); i++) {
			try {
				Person person = new Person();
				person.setId(data.getJSONObject(i).getString("id").toString());
				person.setFirstName(data.getJSONObject(i).getString("firstName").toString());
				person.setLastName(data.getJSONObject(i).getString("lastName").toString());
				person.setEmail(data.getJSONObject(i).getString("email").toString());
				persons.add(person);
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}
		return persons;
	}

	public Person getPersonalData(String url) {
		connect(url);
		Log.i("personal data", personalData.toString());
		Person person = new Person();
		try {
			person.setId(personalData.getString("id"));
			person.setFirstName(personalData.getString("firstName"));
			person.setLastName(personalData.getString("lastName"));
			person.setGender(personalData.getString("male"));
			person.setBirthDate(personalData.getString("birthDate"));
			person.setStatus(personalData.getString("status"));
			person.setAddress(personalData.getString("address"));
			person.setCity(personalData.getString("city"));
			person.setState(personalData.getString("state"));
			person.setCountry(personalData.getString("country"));
			person.setMobile(personalData.getString("mobile"));
			person.setEmail(personalData.getString("email"));

			father = personalData.getJSONObject("father");
			personFather = new Person();
			personFather.setId(father.getString("id"));
			personFather.setFirstName(father.getString("firstName"));
			personFather.setLastName(father.getString("lastName"));
			personFather.setGender(father.getString("male"));
			personFather.setBirthDate(father.getString("birthDate"));
			personFather.setStatus(father.getString("status"));
			personFather.setAddress(father.getString("address"));
			personFather.setCity(father.getString("city"));
			personFather.setState(father.getString("state"));
			personFather.setCountry(father.getString("country"));
			personFather.setMobile(father.getString("mobile"));
			personFather.setEmail(father.getString("email"));
			person.setFather(personFather);

			mother = personalData.getJSONObject("mother");
			personMother = new Person();
			personMother.setId(mother.getString("id"));
			personMother.setFirstName(mother.getString("firstName"));
			personMother.setLastName(mother.getString("lastName"));
			personMother.setGender(mother.getString("male"));
			personMother.setBirthDate(mother.getString("birthDate"));
			personMother.setStatus(mother.getString("status"));
			personMother.setAddress(mother.getString("address"));
			personMother.setCity(mother.getString("city"));
			personMother.setState(mother.getString("state"));
			personMother.setCountry(mother.getString("country"));
			personMother.setMobile(mother.getString("mobile"));
			personMother.setEmail(mother.getString("email"));
			person.setMother(personMother);

		} catch (JSONException e) {
			e.printStackTrace();
		}
		return person;
	}
	public void connect2(String url) {

		HttpClient httpclient = new DefaultHttpClient();

		HttpGet httpget = new HttpGet(url);

		HttpResponse response;
		try {
			response = httpclient.execute(httpget);
			Log.i("Android JSON", response.getStatusLine().toString());
			Log.i("Retrive Data from", url);
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				InputStream instream = entity.getContent();
				String result = convertStreamToString(instream);
				data = new JSONArray(result);
				instream.close();
			}

		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}
	public void connect(String url) {

		HttpClient httpclient = new DefaultHttpClient();

		HttpGet httpget = new HttpGet(url);

		HttpResponse response;
		try {
			response = httpclient.execute(httpget);
			Log.i("Android JSON", response.getStatusLine().toString());
			Log.i("Retrive Data from", url);
			HttpEntity entity = response.getEntity();
			if (entity != null) {
				InputStream instream = entity.getContent();
				String result = convertStreamToString(instream);
				personalData = new JSONObject(result);
				instream.close();
			}

		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (JSONException e) {
			// TODO: handle exception
			e.printStackTrace();
		}
	}

	public String convertStreamToString(InputStream is) {

		BufferedReader reader = new BufferedReader(new InputStreamReader(is));
		StringBuilder sb = new StringBuilder();

		String line = null;
		try {
			while ((line = reader.readLine()) != null) {
				sb.append(line + "\n");
			}
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				is.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return sb.toString();
	}
}